#ifndef GRABBER_H
#define GRABBER_H
#include <QCoreApplication>

//! Macros.
#define MAX_DAYS 14
#define MAX_OFFSET 14
#define APPNAME "tv_grab_fi_m"

//! Forward declarations.
class QTextStream;
class QNetworkAccessManager;
class QNetworkReply;
class QSettings;

//! App class.
class TvGrabber : public QCoreApplication
{
    Q_OBJECT

public:
    /* Enum for option keys. */
    enum OptionKey
    {
        keyNone=0,
        keyQuiet,
        keyDescription,
        keyCapabilities,
        keyConfigure,
        keyConfigFile,
        keyOutput,
        keyDays,
        keyOffset
    };

    /* Enum for URL types. */
    enum UrlType
    {
        urlTypeNone=0,
        urlTypeMain,
        urlTypeChannelGroup,
        // Keep this as the last item.
        URL_TYPE_COUNT
    };

    //! CTOR
    TvGrabber(int argc, char **argv);

    //! DTOR
    ~TvGrabber();

    //! Getter for qout.
    QTextStream &cout() {return *qout;}

    //! Reconfigure the app.
    void reconfigure();

    //! Setter for silent.
    void setSilent(const bool &other) { silent = other; }

    //! Setter for offset.
    void setOffset(const int &other) { offset = other; }

    //! Setter for days.
    void setDays(const int &other) { days = other; }

    //! Setter for output file.
    void setOutputFile(const char *other) { outputFile = QString(other); }

    //! Setter for config file.
    void setConfigFile(const char *other) { configFile = QString(other); }

    //! Setter for suddenDeath.
    void setSuddenDeath(const OptionKey &other) { suddenDeath = other; }

    //! Getter for suddenDeath.
    OptionKey getSuddenDeath() { return suddenDeath; }

public slots:

    //! Network access finished.
    void netAccessFinished(QNetworkReply * reply);

    //! Deferred init.
    void deferredInit();

private:

    //! Main URL's network access finished.
    void mainUrlAccessFinished(QNetworkReply * reply);

    //! Network access finished.
    void channelGroupAccesFinished(QNetworkReply *reply);

    //! Compress received data.
    QString compressResponse(QByteArray responseBytes);

    //! D A T A

    //! Stdout as a text stream.
    QTextStream *qout;

    //! Settings object.
    QSettings *settings;

    //! Output file.
    QString outputFile;

    //! Config file.
    QString configFile;

    //! Integer to disable event loop entry.
    OptionKey suddenDeath;

    //! Do not print anything.
    bool silent;

    //! Days to grab.
    int days;

    //! Offset in days where to start grabbing.
    int offset;

    //! Network access manager.
    QNetworkAccessManager *nam;

    //! Main URL.
    QString mainUrl;
};

#endif // GRABBER_H
